/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

let Utility = {}
  // var assign, getValue, isArray, isEmpty, isFunction, isObject, isPlainObject,
    let slice = [].slice;
    let hasProp = {}.hasOwnProperty;

let assign = function () {
  var i, key, len, source, sources, target;
  target = arguments[0], sources = 2 <= arguments.length ? slice.call(arguments, 1) : [];
  if (isFunction(Object.assign)) {
    Object.assign.apply(null, arguments);
  } else {
    for (i = 0, len = sources.length; i < len; i++) {
      source = sources[i];
      if (source != null) {
        for (key in source) {
          if (!hasProp.call(source, key)) continue;
          target[key] = source[key];
        }
      }
    }
  }
  return target;
};

let isFunction = function (val) {
  return !!val && Object.prototype.toString.call(val) === '[object Function]';
};

let isObject = function (val) {
  var ref;
  return !!val && ((ref = typeof val) === 'function' || ref === 'object');
};

let isArray = function (val) {
  if (isFunction(Array.isArray)) {
    return Array.isArray(val);
  } else {
    return Object.prototype.toString.call(val) === '[object Array]';
  }
};

let isEmpty = function (val) {
  var key;
  if (isArray(val)) {
    return !val.length;
  } else {
    for (key in val) {
      if (!hasProp.call(val, key)) continue;
      return false;
    }
    return true;
  }
};

let isPlainObject = function (val) {
  var ctor, proto;
  return isObject(val) && (proto = Object.getPrototypeOf(val)) && (ctor = proto.constructor) && (typeof ctor === 'function') && (ctor instanceof ctor) && (Function.prototype.toString.call(ctor) === Function.prototype.toString.call(Object));
};

let getValue = function (obj) {
  if (isFunction(obj.valueOf)) {
    return obj.valueOf();
  } else {
    return obj;
  }
};

Utility.assign = assign;

Utility.isFunction = isFunction;

Utility.isObject = isObject;

Utility.isArray = isArray;

Utility.isEmpty = isEmpty;

Utility.isPlainObject = isPlainObject;

Utility.getValue = getValue;

export { Utility }
